<?php
//登录、注册、退出
namespace App\Http\Controllers\Admin\Mobile;

use App\Models\User\UsersInfo;
use Illuminate\Http\Request;
use App\Modules\Mobile\Auth;
use App\Http\Controllers\AdapterWebController;
use App\Http\Controllers\Admin\AliController;

class AuthController extends AdapterWebController
{
    // 移动端 初次登陆
    public function login(Request $request)
    {
        session()->forget('agent_name');
        session()->forget('agent_id');
        session()->forget('software_id');
        session()->forget('second');
        if ($request->isMethod('get')) {
            return view('admin.login.mobile.login');
        }
        // 提交数据
        $postData = $request->all();
        $postData['password'] = md5($postData['password']);
        // post 提交
        $info = $this->appLayerExec(Auth::class, 'login', $postData);
        // 登录成功
        if($info['code'] == 200) {
            $request->session()->put('agent_name', $info['res']['user_name']);
            $request->session()->put('agent_id', $info['res']['id']);
            $request->session()->put('software_id', $info['res']['software_id']);
            $request->session()->put('second', 0);
            return redirect('admin/home');
        }

        return throw_exception($info['msg']);
    }
    // 退出登录
    public function logout()
    {
        session()->forget('agent_name');
        session()->forget('agent_id');
        session()->forget('software_id');
        session()->forget('second');
        return redirect('admin/mobileLogin');
    }
    //   修改密码
    public function amendPassword(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('admin.agent.mobile.amend_password', ['status' => 0]);
        }

        if ($request['new_password'] == $request['old_password']) {
            return throw_exception('新密码和旧密码不能相同!');
        }
        if ($request['new_password'] != $request['check_password']) {
            return throw_exception('新密码两次输入不一致!');
        }
        $postData['id'] = session('agent_id');
        $postData['password'] = $request['old_password'];
        $res = $this->appLayerExec(Auth::class, 'checkIdentity', $postData);
        if($res['code'] != 200) {
            return throw_exception('原密码匹配失败，请重试！');
        }
        $postData['password'] = $request['new_password'];
        $rst = $this->appLayerExec(Auth::class, 'updateInfo', $postData);
        if($rst['code'] == 200) {
            return view('admin.agent.mobile.amend_password', ['status' => 1]);
        }
        return throw_exception('你密码修改异常，请稍后重试！');
    }
    // 找回密码
    public function findPassword(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('admin.agent.mobile.find_password', ['status' => 0]);
        }
        $checkData['phone'] = $request['phone'];
        $res = $this->appLayerExec(Auth::class, 'checkPhone', $checkData);
        if ($res['code'] != 200) {
            throw_exception('手机号码匹配失败，请重试！');
        }
        $redis = app("redis");
        $code = $redis->get($request['phone']);
        if ($request['code'] != $code) {
            throw_exception('验证码不正确，请重试！');
        }
        if ($request['new_password'] != $request['check_password']) {
            return throw_exception('新密码两次输入不一致!');
        }
        $postData['id'] = $res['res']['id'];
        $postData['password'] = $request['new_password'];
        $rst = $this->appLayerExec(Auth::class, 'updateInfo', $postData);
        if($rst['code'] == 200) {
            return view('admin.agent.mobile.find_password', ['status' => 1]);
        }
        return throw_exception('你密码修改异常，请稍后重试！');
    }
    //    注册
    public function registrationWeb(Request $request)
    {
        if ($request->isMethod('get')) {
            $info = [
                'status' => 0,
                'real_name' => '',
                'stage_name' => '',
                'phone' => '',
            ];
            return view('admin.agent.mobile.registration', $info);
        }
        $postData = array_filter($request->all());
        $redis = app("redis");
        $code = $redis->get($postData['phone']);
        if ($code != $postData['code']) {
            return throw_exception('验证码不正确!');
        }
        $userInfo = UsersInfo::where(['phone'=>$postData['phone']])->first();
        if($userInfo){
            $userInfo = $userInfo->toArray();
        }
        if(!empty($userInfo)){
            return throw_exception('该手机号已注册');
        }

        $userInfo = UsersInfo::where(['phone'=>$postData['card']])->first();
        if($userInfo){
            $userInfo = $userInfo->toArray();
        }
        if(!empty($userInfo)){
            return throw_exception('该身份证已注册');
        }
        $postData['software_id'] = session('software_id');
        $postData['type'] = 1;
        if (isset($postData['agreement'])) {
            $fileRes = $this->fileUpload($request, 'agreement');
            if (!$fileRes['status']) {
                return throw_exception('请上传协议');
            }
            $postData['agreement'] = $fileRes['url'];
        }
        $rst = $this->appLayerExec(Auth::class, 'register', $postData);
        if ($rst['status'] == 1) {
            AliController::aliSmsAccount($postData['phone']);
            $info = [
                'status' => 1,
                'real_name' => $postData['real_name'],
                'stage_name' => $postData['stage_name'],
                'phone' => $postData['phone'],
            ];
            return view('admin.agent.mobile.registration', $info);
        }
        return throw_exception('注册失败');
    }
    // 首页
    public function home()
    {
        return view('admin.home.mobile.home');
    }
    // 二级密码
    public function secondPassword(Request $request)
    {
        if (session('second') == 0) {
            if ($request->isMethod('get')) {
                return view('admin.agent.mobile.second_password');
            }
            $postData['id'] = session('agent_id');
            $postData['password'] = $request['password'];
            $res = $this->appLayerExec(Auth::class, 'checkIdentity', $postData);
            if($res['code'] == 200) {
                $request->session()->put('second', 1);
                return redirect('admin/person');
            }
            return throw_exception('身份验证失败，请稍后重试！');
        }
        return redirect('/admin/person');
    }
}
